package tkaczyk.simtalk.android;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class MainActivity extends Activity implements OnClickListener, OnEditorActionListener {

	private static String TAG = "std.MainActivity";
	
	private Button btnConnect;
	private Button btnListen;
	private Button btnPrefs;
	
	private TextView txtStatus;
	private ListView chatArea;
	private EditText sendArea;
	
	private SimTalkDroid app;

	/**
	 * Called when the activity is first created.
	 * 
	 * @param savedInstanceState
	 *            If the activity is being re-initialized after previously being
	 *            shut down then this Bundle contains the data it most recently
	 *            supplied in onSaveInstanceState(Bundle). <b>Note: Otherwise it
	 *            is null.</b>
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.i(TAG, "onCreate");
		setContentView(R.layout.main);
		
		app = (SimTalkDroid) super.getApplication();
		
		btnConnect = (Button) findViewById(R.id.btnConnect);
		btnListen = (Button) findViewById(R.id.btnListen);
		btnPrefs = (Button) findViewById(R.id.btnPrefs);
		
		btnConnect.setOnClickListener(this);
		btnListen.setOnClickListener(this);
		btnPrefs.setOnClickListener(this);
		
		txtStatus = (TextView) findViewById(R.id.txtStatus);
		sendArea = (EditText) findViewById(R.id.sendArea);
		sendArea.setOnEditorActionListener(this); 
		chatArea = (ListView) findViewById(R.id.chatArea);
		chatArea.setAdapter(app.getAdapter());
		
		DisplayUpdate receiver = new DisplayUpdate();
		IntentFilter filter = new IntentFilter();
		filter.addAction(ChatService.STATUS_UPDATE);
		filter.addAction(ChatService.CHAT_UPDATE);
		registerReceiver(receiver, filter);
	}

	@Override
	public void onClick(View v) {
		Log.d(TAG, "button clicked");
		switch (v.getId()) {
		case R.id.btnConnect:
			connect();
			break;
		case R.id.btnListen:
			listen();
			break;
		case R.id.btnPrefs:
			launchPrefs();
			break;
		}
	}

	private void launchPrefs() {
		Log.i(TAG, "launching preferences activity");
		startActivity(new Intent(this, PrefsActivity.class));
	}

	private void listen() {
		disableButtons();
		if (app.isListening()) {
			app.stopListening();
		} else {
			app.listen();
		}
	}

	private void connect() {
		disableButtons();
		if (app.isConnected()) {
			app.disconnect();
		} else {
			app.connect();
		}
	}
	
	class DisplayUpdate extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction() == ChatService.STATUS_UPDATE) {
				updateStatusView();
			}
			if (intent.getAction() == ChatService.CHAT_UPDATE) {
				updateChatView();
			}
			updateButtons();
		}
	}
	
	@Override
	public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
		if (actionId == EditorInfo.IME_ACTION_SEND) {
			app.sendMessage(v.getText().toString());
			v.setText("");
			InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
			return true;
		}
		return false;
	}
	
	private void updateStatusView() {
		txtStatus.setText(app.getStatusMessage());
	}
	
	private void updateChatView() {
	}
	
	private void updateButtons() {
		if (app.isConnected()) {
			btnConnect.setText(R.string.disconnect);
			btnListen.setText(R.string.listen);
			btnConnect.setEnabled(true);
			btnListen.setEnabled(false);
		} else if (app.isListening()) {
			btnConnect.setText(R.string.disconnect);
			btnListen.setText(R.string.stopListen);
			btnConnect.setEnabled(false);
			btnListen.setEnabled(true);
		} else {
			btnConnect.setText(R.string.connect);
			btnListen.setText(R.string.listen);
			btnConnect.setEnabled(true);
			btnListen.setEnabled(true);
		}
	}
	
	private void disableButtons() {
		btnConnect.setEnabled(false);
		btnListen.setEnabled(false);
	}
	
}
